package com.sean.server.controller;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseController {
	@Autowired  
	protected  HttpServletRequest request;  
	
	protected PageResult res = new PageResult();
	
	protected String getIpAddr() {
		String ip = request.getHeader("x-forwarded-for");
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "localhost";
		}
		return ip;
	}
	
	/**
	 * 错误回复.
	 * @param msg
	 * @return
	 */
	protected PageResult getErrRes(int code, String msg) {
		return new PageResult(code, msg);
	}
	
	/**
	 * 错误签名.
	 * @param msg
	 * @return
	 */
	protected PageResult getSignErrRes() {
		return new PageResult(1, "签名验证失败!");
	}
	
	/**
	 * 错误参数.
	 * @param msg
	 * @return
	 */
	protected PageResult getParamErrRes() {
		return new PageResult(3, "参数错误!");
	}
	
	/**
	 * ajax返回处理结果
	 * @author XinWang
	 *
	 */
	public class PageResult extends HashMap {
		public PageResult() {
			this.put("code", 0);
		}
		/**
		 * 处理出错结果集创建.
		 * @param errorCode
		 * @param errorTextKey
		 */
		public PageResult(int errorCode, String errorTextKey) {
			this.put("code", errorCode);
			this.put("msg", errorTextKey);
		}
		public PageResult(int errorCode) {
			this.put("code", errorCode);
		}
	}
}
